import Vue from 'vue'
import Vuex from 'vuex'
import {changeTheme, changeThemeByName} from "@/helper/theme";

Vue.use(Vuex)

export default new Vuex.Store({
    state: {

        /* status */
        active: null,
        show: false,
        loading: false,
        mapLoading: true,
        provinceLoading: true,
        chinaLoading: true,

        /* info */
        user: {
            sid: null,
            password: null,
            info: null
        },
        reports: [],
        settings: {
            switchSetting: {
                history: {
                    value: true,
                    info: {
                        intro: '开启历史记录，下次登录时搜索记录仍然存在',
                        icon: 'icon-lishijilu',
                        name: '历史记录',
                        color: 'rgba(15,17,16,0.5)',
                    }
                },
                provinceMap: {
                    value: true,
                    info: {
                        intro: '开启可查看省份地图',
                        icon: 'icon-province',
                        name: '省份地图',
                        color: 'rgba(255,102,30,0.5)',
                    }
                }
            },
            theme: {
                value: 'neumorphism',
                info: {
                    intro: '主题选择',
                    icon: 'icon-zhuti',
                    name: '主题'
                }
            }
        }
    },
    mutations: {
        /**
         * 初始化
         * @param state
         */
        initVolcreateEpidemic(state) {
            let settings = JSON.parse(localStorage.getItem("volcreate_epidemic_settings")) || {}
            state.settings = {
                ...state.settings,
                ...settings
            }
            if (!state.settings.switchSetting.history.value) {
                localStorage.removeItem("volcreate_epidemic_reports")
            } else {
                state.reports = JSON.parse(localStorage.getItem("volcreate_epidemic_reports")) || []
            }
            changeThemeByName(state.settings.theme.value);
        },
        saveUser(state, user) {
            state.user = user
        },
        /**
         * 在添加元素的同时，将reports保存在本地（settings.history = true）
         * @param state
         * @param report
         */
        addReport(state, report) {
            state.reports.unshift(report)
            state.active = 0;
            if (state.settings.switchSetting.history.value) {
                localStorage.setItem("volcreate_epidemic_reports", JSON.stringify(state.reports))
            }
        },
        setActive(state, active) {
            if (active != null) {
                if (state.show && state.reports[active].allInfo == null) {
                    state.show = false;
                }
            }
            state.active = active
        },
        setShow(state, show) {
            state.show = show
        },
        setLoading(state, loading) {
            state.loading = loading
        },
        setMapLoading(state, loading) {
            state.mapLoading = loading
        },
        setChinaLoading(state, loading) {
            state.chinaLoading = loading
        },
        setProvinceLoading(state, loading) {
            state.provinceLoading = loading
        },
        changeTheme(state, name) {
            state.settings.theme.value = name;
            changeThemeByName(name);
        }

    },
    actions: {},
    modules: {}
})
